package com.tea.funct.controller;

import com.tea.common.annotation.Log;
import com.tea.common.core.controller.BaseController;
import com.tea.common.core.domain.AjaxResult;
import com.tea.common.core.page.TableDataInfo;
import com.tea.common.enums.BusinessType;
import com.tea.common.utils.poi.ExcelUtil;
import com.tea.funct.domain.TeaSubclass;
import com.tea.funct.service.ITeaSubclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 产品小类Controller
 *
 * @author Sophia
 * @date 2024-05-25
 */
@RestController
@RequestMapping("/funct/subclass")
public class TeaSubclassController extends BaseController {
    @Autowired
    private ITeaSubclassService teaSubclassService;

    /**
     * 查询产品小类列表
     */
    @PreAuthorize("@ss.hasPermi('funct:subclass:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeaSubclass teaSubclass) {
        startPage();
        List<TeaSubclass> list = teaSubclassService.selectTeaSubclassList(teaSubclass);
        return getDataTable(list);
    }

    /**
     * 查询所有产品小类列表
     */
    @PreAuthorize("@ss.hasPermi('funct:subclass:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(@RequestParam(value = "categoryId", required = false) Long categoryId) {
        List<TeaSubclass> list = teaSubclassService.selectTeaSubclassListAll(categoryId);
        return getDataTable(list);
    }

    /**
     * 导出产品小类列表
     */
    @PreAuthorize("@ss.hasPermi('funct:subclass:export')")
    @Log(title = "产品小类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TeaSubclass teaSubclass) {
        List<TeaSubclass> list = teaSubclassService.selectTeaSubclassList(teaSubclass);
        ExcelUtil<TeaSubclass> util = new ExcelUtil<TeaSubclass>(TeaSubclass.class);
        util.exportExcel(response, list, "产品小类数据");
    }

    /**
     * 获取产品小类详细信息
     */
    @PreAuthorize("@ss.hasPermi('funct:subclass:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(teaSubclassService.selectTeaSubclassById(id));
    }

    /**
     * 新增产品小类
     */
    @PreAuthorize("@ss.hasPermi('funct:subclass:add')")
    @Log(title = "产品小类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeaSubclass teaSubclass) {
        return toAjax(teaSubclassService.insertTeaSubclass(teaSubclass));
    }

    /**
     * 修改产品小类
     */
    @PreAuthorize("@ss.hasPermi('funct:subclass:edit')")
    @Log(title = "产品小类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeaSubclass teaSubclass) {
        return toAjax(teaSubclassService.updateTeaSubclass(teaSubclass));
    }

    /**
     * 删除产品小类
     */
    @PreAuthorize("@ss.hasPermi('funct:subclass:remove')")
    @Log(title = "产品小类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(teaSubclassService.deleteTeaSubclassByIds(ids));
    }
}
